﻿using System;
using System.Collections.Generic;
using System.Text;
using ITAP.ShprossesBuilder.Modelling.Components;
using System.Drawing;
using ITAP.ShprossesBuilder.Modelling.Components.Actions;
using ITAP.ShprossesBuilder.Modelling;
using ITAP.Core.Modelling;

namespace ITAP.ShprossesBuilder.Manipulation
{
    public class CommandEditorBreakComponent : SerializedUndoRedo, IModelComponentCommand
    {
        private double gapBetweenPieces = 10;

        public double GapBetweenPieces
        {
            get { return gapBetweenPieces; }
            set { gapBetweenPieces = value; }
        }
        private MouseManagerEvent mouseEvent;
        private Point mouseDownPoint;
        private List<Type> allowedComponentTypes;

        public CommandEditorBreakComponent(ModelControl modelControl, Type[] allowedComponentTypes)
        {
            this.modelControl = modelControl;
            this.allowedComponentTypes = new List<Type>(allowedComponentTypes);
        }

        #region IModelComponentCommand Members

        public List<ITAP.ShprossesBuilder.Modelling.Components.IComponent> ComponentsForProcessing
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        #endregion

        #region IModelControlCommand Members

        public MouseManagerEvent MouseEvent
        {
            get
            {
                return mouseEvent;
            }
            set
            {
                mouseEvent = value;
            }
        }

        public ModelControl ModelControl
        {
            get
            {
                return modelControl;
            }
            set
            {
                modelControl = value;
            }
        }

        public System.Drawing.Point MouseDownPoint
        {
            get
            {
                return mouseDownPoint;
            }
            set
            {
                mouseDownPoint = value;
            }
        }

        public System.Drawing.Point MouseUpPoint
        {
            get
            {
                return mouseDownPoint;
            }
            set
            {
                mouseDownPoint = value;
            }
        }

        public System.Drawing.Point MouseMovePoint
        {
            get
            {
                return mouseDownPoint;
            }
            set
            {
                mouseDownPoint = value;
            }
        }

        public bool ExecuteImmediately
        {
            get
            {
                return false;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        #endregion

        #region ICommand Members

        public void StartExecute()
        {
            undoModelContainer = (ModelContainer)modelControl.ModelContainer.Clone();

            ModelPoint point = modelControl.ScreenToModel(mouseDownPoint);
            List<IComponent> componentsByPoint = modelControl.ModelContainer.GetComponentsByPoint(point, 10);
            
            IComponentAction breakAction = new ActionBreakInPoint(point, gapBetweenPieces);
            foreach (IComponent component in componentsByPoint)
                if (allowedComponentTypes.Contains(component.GetType()))
                    breakAction.Action((IEndedComponent)component);            
        }

        public void Execute()
        {
            //throw new NotImplementedException();
        }

        public void EndExecute()
        {
            //throw new NotImplementedException();
        }

        public bool IsNeedModelUpdate
        {
            get { return true; }
        }

        public string Name
        {
            get { return "Разбить профиль на части"; }
        }

        #endregion

        #region ICloneable Members

        public object Clone()
        {
            CommandEditorBreakComponent clone = new CommandEditorBreakComponent(modelControl, allowedComponentTypes.ToArray());
            clone.gapBetweenPieces = this.gapBetweenPieces;
            clone.mouseDownPoint = this.mouseDownPoint;

            if (this.undoModelContainer != null)
                clone.undoModelContainer = (ModelContainer)this.undoModelContainer.Clone();
            if (this.redoModelContainer != null)
                clone.redoModelContainer = (ModelContainer)this.redoModelContainer.Clone();

            return clone;
        }

        #endregion

        public override string ToString()
        {
            return this.Name;
        }

        #region ICommand Members


        private bool enabled = true;
        public bool Enabled
        {
            get
            {
                return enabled;
            }
            set
            {
                enabled = value;
            }
        }

        #endregion

        #region ICommand Members


        public bool IsNeedVisitorsUpdate
        {
            get { return true; }
        }

        #endregion
    }
}
